package com.woniuxy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.RbacPermDao;
import com.woniuxy.entity.RbacPerm;
import com.woniuxy.entity.RbacRole;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.RbacPermService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@Service
public class RbacPermServiceImpl implements RbacPermService {
    @Resource
    RbacPermDao rbacPermDao;

    @Override
    public ResponseEntity findAll(int currentPage,int pageSize) {
        PageHelper.startPage(currentPage, pageSize);//只要在这里传入了分页信息
        List<RbacPerm>list =rbacPermDao.findAll();
        PageInfo pageInfo = new PageInfo(list);//就能够封装分页信息
        return new ResponseEntity(200,"查询成功",pageInfo);
    }
    @Override
    public ResponseEntity findOne(int id) {
        RbacPerm one = rbacPermDao.findOne(id);
        return new ResponseEntity(200, "查询成功",one);
    }

    @Override
    public ResponseEntity addOne(RbacPerm rbacPerm) {
        int i = rbacPermDao.addOne(rbacPerm);
        if (i > 0) {
            return new ResponseEntity(200,"成功增加角色名为为"+rbacPerm.getName()+"的信息",null);
        }else {
            return  new ResponseEntity(500,"增加角色名为为"+rbacPerm.getName()+"的信息失败",null);
        }
    }

    @Override
    public ResponseEntity deleteOne(int id) {
        int i = rbacPermDao.deleteOne(id);
        if (i > 0) {
            return new ResponseEntity(200,"成功删除id为"+id+"的信息",null);
        }else {
            return  new ResponseEntity(500,"删除id为"+id+"的信息失败",null);
        }
    }

    @Override
    public ResponseEntity updateOne(RbacPerm rbacPerm) {
        int i = rbacPermDao.updateOne(rbacPerm);
        if (i>0) {
            return new ResponseEntity(200, "编辑成功", null);
        }else {
            return new ResponseEntity(500, "编辑失败", null);
        }
    }

    @Override
    public ResponseEntity search(int currentPage, int pageSize,String key,String key1) {
        Page<Object> objects = PageHelper.startPage(currentPage, pageSize);
        List<RbacPerm> keyObject= rbacPermDao.search(key,key1);
        PageInfo pageInfo=new PageInfo(keyObject);
        return new ResponseEntity(200,"查询成功",pageInfo);
    }

    @Override
    public Set<String> findPermBySetRoles(String username) {
        return rbacPermDao.findPermBySetRoles(username);
    }


}

